package hu.berczi.Util.System;

import java.io.IOException;

public class SysUtil {
	/**
	 * Tries to exec the command, waits for it to finsih, logs errors if exit
	 * status is nonzero, and returns true if exit status is 0 (success).
	 * 
	 * @param command
	 *            Description of the Parameter
	 * @return Description of the Return Value
	 */
	public static boolean exec(String command) {
		Process proc;

		try {
			// System.out.println("Trying to execute command " +
			// Arrays.asList(command));
			proc = Runtime.getRuntime().exec(command);
		} catch (IOException e) {
			// java.lang.System.out.println("IOException while trying to execute "
			// +
			// command);
			return false;
		}

		// System.out.println("Got process object, waiting to return.");

		int exitStatus;
		while (true) {
			try {
				exitStatus = proc.waitFor();
				break;
			} catch (java.lang.InterruptedException e) {
				// System.out.println("Interrupted: Ignoring and waiting");
			}
		}

		if (exitStatus != 0) {
			System.err.println("Error executing command: (exitStatus:"
					+ exitStatus + ") " + command);
		}
		return (exitStatus == 0);
	}

	public static void execNoReturn(String command) throws IOException {
		Runtime.getRuntime().exec(command);
	}

}
